import { RouteRecordRaw, createRouter, createWebHistory } from 'vue-router';
import routes from '@/router/router.default';

const autoRouters = () => {
  const pages = import.meta.glob('../views/page/**/*.vue');
  
  Object.entries(pages).forEach(([page, component]) => {
    const addr = page.replace('../views/page', '').split('/').filter(Boolean);
    const paths = addr.map((_, index) => `/${addr.slice(0, index + 1).join('/')}`);
    
    paths.forEach(path => {
      let name = path.slice(path.lastIndexOf('/') + 1).replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
      name = name.replace(/\.vue$/, '');

      if (!routes.some(route => route.name === name)) {
        routes.push({ name, path: `/page${path}`, redirect: '', component, children: [] });
      }
    });
  });
  console.log(routes)


  
  return routes;
};

autoRouters();

const router = createRouter({
  history: createWebHistory(),
  routes
});

export default router;